package gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import Usager.AbstractCompte;
import Usager.ListeCompte;
import javax.swing.JPasswordField;

import carnetCommandes.Registre;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Login extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textUsager;
	private JFrame frame;
	private JPanel buttonPane;
	private JPasswordField passwordField;


	/**
	 * Launch the application.
	 */

	/**
	 * Create the dialog.
	 */
	public Login(JFrame framef) {
		frame = framef;
		setBounds(100, 100, 231, 169);
		setResizable(false);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		this.setTitle("Zone Client");
		contentPanel.setLayout(null);
		
		{
			JLabel lblUsager = new JLabel("Usager");
			lblUsager.setBounds(10, 11, 77, 14);
			contentPanel.add(lblUsager);
		}
		{
			textUsager = new JTextField();
			textUsager.setBounds(97, 11, 105, 20);
			contentPanel.add(textUsager);
			textUsager.setColumns(10);
		}
		{
			JLabel lblMotDePasse = new JLabel("Mot de passe");
			lblMotDePasse.setBounds(10, 43, 77, 14);
			contentPanel.add(lblMotDePasse);
		}
		
		JLabel lblSinscrire = new JLabel("S'inscrire");
		lblSinscrire.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Inscription creerCompte = new Inscription(frame);
				creerCompte.setVisible(true);
			}
		});
		lblSinscrire.setForeground(Color.BLUE);
		lblSinscrire.setBounds(97, 73, 105, 14);
		contentPanel.add(lblSinscrire);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(97, 40, 105, 20);
		contentPanel.add(passwordField);
		{
			buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0){
						
						String password = String.valueOf(passwordField.getPassword());
						AbstractCompte compte = ListeCompte.getInstance().getCompte(textUsager.getText(), password);
						int response = 0;

					if (compte != null){
						
						Registre.getInstance().setCompteActif(compte);
						PanelFactory factory = PanelFactory.getInstance();
						frame.setContentPane(factory.CreePanel(compte.getClass().getName()));
						Registre registre = Registre.getInstance();
						registre.setCompteActif(compte);
						frame.setVisible(true);

					}
					else {
							

							// Message d'erreur
							response = JOptionPane.showOptionDialog(getContentPane(), "Aucun identificateur correspondant, voulez-vous creer un usager ?", 
														 "Attention", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, 
														 null, null, 0);					
					}
						
						frame.validate();
						frame.repaint();
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						System.exit(0);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
}
